package net.csdn.business.notify.common.mapper;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;

import net.csdn.business.notify.common.model.entity.NotifyAnnounce;
import net.csdn.business.notify.common.model.query.NotifyAnnounceQuery;
import net.csdn.business.notify.common.model.vo.NotifyAnnounceVO;
import net.csdn.business.notify.common.model.vo.UserAnnounceVO;
import org.apache.ibatis.annotations.Delete;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;

import java.util.List;

/**
 * 通知中心-系统公告Mapper接口
 * 
 * @author ruoyi
 * @date 2023-11-27
 */
public interface NotifyAnnounceMapper extends BaseMapper<NotifyAnnounce>
{

    /**
     * 分页查询通知中心-系统公告列表
     *
     * @param query 字典类型信息
     * @return 字典类型集合信息
     */
    @Select("<script> select * from notify_announce where 1=1 " +
            "<if test='query.title != null and query.title!=\"\"' >" +
            " AND title like concat('%', #{query.title}, '%')  " +
            "</if>" +
            "<if test='query.content != null and query.content!=\"\"' >" +
            " AND content like concat('%', #{query.content}, '%')  " +
            "</if>" +
            "<if test='query.createdBy != null and query.createdBy!=\"\"' >" +
            " AND created_by=#{query.createdBy} " +
            "</if>" +
            " order by created_date desc " +
            "</script>")
    public List<NotifyAnnounceVO> findPageList(Page page, @Param("query") NotifyAnnounceQuery query);

    /**
     * 查询通知中心-系统公告
     * 
     * @param id 通知中心-系统公告主键
     * @return 通知中心-系统公告
     */
    @Select("<script> select * from notify_announce where object_id= #{id} </script>")
    public NotifyAnnounceVO selectNotifyAnnounceById(String id);

    /**
     * 查询通知中心-系统公告列表
     * 
     * @param query 通知中心-系统公告
     * @return 通知中心-系统公告集合
     */
    @Select("<script> select * from notify_announce where 1=1 " +
            "<if test='query.title != null and query.title!=\"\"' >" +
            " AND title like concat('%', #{query.title}, '%')  " +
            "</if>" +
            "<if test='query.content != null and query.content!=\"\"' >" +
            " AND content like concat('%', #{query.content}, '%')  " +
            "</if>" +
            "<if test='query.createdBy != null and query.createdBy!=\"\"' >" +
            " AND created_by=#{query.createdBy} " +
            "</if>" +
            " order by created_date desc " +
            "</script>")
    public List<NotifyAnnounceVO> selectNotifyAnnounceList(@Param("query") NotifyAnnounceQuery query);



    /**
     * 删除通知中心-系统公告
     * 
     * @param id 通知中心-系统公告主键
     * @return 结果
     */
    @Delete("<script> delete from notify_announce where object_id= #{id} </script>")
    public Integer deleteNotifyAnnounceById(String id);


    /**
     * 前台用户分页查询系统公告
     *
     * @param query 字典类型信息
     * @return 字典类型集合信息
     */
    @Select("<script> select * from (select na.id, na.object_id,na.title,na.content,case when nau.receiver_id  is null then 0 else 1 end as read_state,na.created_by,na.created_date,na.last_modified_by,na.last_modified_date " +
            " from notify_announce na left join notify_announce_user nau on na.object_id = nau.announce_id and nau.receiver_id=#{query.currUserId}  ) ann order by ann.read_state,ann.created_date desc </script>")
    public List<UserAnnounceVO> userAnnouncePage(Page page, @Param("query") NotifyAnnounceQuery query);



    @Select("<script> select * from notify_announce where object_id=#{query.id} " +
            "</script>")
    public UserAnnounceVO userAnnounceDetail(@Param("query")NotifyAnnounceQuery query);



    @Select("<script> select sum(case when nau.receiver_id  is null then 1 else 0 end) as total " +
            " from notify_announce na left join notify_announce_user nau on na.object_id = nau.announce_id and nau.receiver_id=#{currUserId} " +
            "</script>")
    public int getUnReadCount(@Param("currUserId")String currUserId);

}
